const amap = uniCloud.importObject('uni-map-co', {
	customUI: true
});
import {
	getGridCode
} from './getSurroundingGrids';
import store from '../store';

export default async function getPois(lng, lat, keyword, radius) {
	// 定义必要的参数


	const key = "2642eb892bac2acb0eeacc2dc524ff1b";
	//const key ='f11PTHhGFVSRVFT1cZRjMEdCnMOAlQXB'
	const keywords = keyword;
	const types = "";
	const city = "";
	const sortrule = "distance";
	const offset = 25;
	const show_fields = "business,photos"
	const page = 1;

	// 拼接正确的 location 参数
	console.log("+++++++++" + lng, lat, keyword, radius);

	// 构建请求 URL
	const url =
		`https://restapi.amap.com/v5/place/around?keywords=${keywords}&key=${key}&location=${lng},${lat}&radius=${radius}&types=${types}&offset=${offset}&page=${page}&sortrule=${sortrule}&show_fields=${show_fields}`;
	//const url= `https://api.map.baidu.com/place/v2/search?query=商住小区$楼宇&location=${lat},${lng}&radius=2000&output=json&ak=${key}`
	try {
		// 发起请求
		const res = await uni.request({
			url: url,
			header: {
				'Content-Type': 'application/json' // 设置合适的请求头
			},
			data: {}
		});

		// 返回请求结果
		return res;
	} catch (error) {
		// 处理请求错误
		console.error('请求周边 POI 信息时出错:', error);
		throw error;
	}
}
export const regeo = async (lng, lat) => {

	const key = "2642eb892bac2acb0eeacc2dc524ff1b";
	const url = `https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${lng},${lat}`
	try {
		// 发起请求
		const [, res] = await uni.request({
			url: url,
			header: {
				'Content-Type': 'application/json' // 设置合适的请求头
			},
			data: {}
		});
		// 返回请求结果
		return {
			name: res.data.regeocode.addressComponent.streetNumber.street || '无地名',
			address: res.data.regeocode.formatted_address
		}

	} catch (error) {
		// 处理请求错误
		console.error('请求周边 POI 信息时出错:', error);
		throw error;
	}
}
export const getGpsFromPoi = async (city, word) => {
	const key = "2642eb892bac2acb0eeacc2dc524ff1b";
	const url =
		`https://restapi.amap.com/v3/place/text?key=${key}&keywords=${word}&citylimit=true&region=${city}`
	try {
		// 发起请求
		const [, res] = await uni.request({
			url: url,
			header: {
				'Content-Type': 'application/json' // 设置合适的请求头
			},
			data: {}
		});
		// 返回请求结果
		return res.data.pois



	} catch (error) {
		// 处理请求错误
		console.error('请求地址经纬度:', error);
		throw error;
	}
}
/**
 * @param {Object} location 含latitude,longitude
 */
export async function location2address(location) {

	const addressInstore = uni.getStorageSync('poiAddress') || []
	const grid = getGridCode(location);
	
	// 查找缓存中是否存在
	const list=addressInstore.filter(item=>item.address&&item.address.length>0)

	const cachedItem = list.find(item => item.grid === grid&&item.address&&item.address.length>0);
    
	if (cachedItem) {
		return {
			address: cachedItem.address,
			addressShort: cachedItem.addressShort
		};
	}

	try {

		const res = await amap.location2address(location);
	    const newaddress=[...addressInstore,res]
	
        uni.setStorageSync('poiAddress', newaddress);
		
		  
		
		return {
			address: res.address,
			addressShort: res.addressShort
		}

	} catch (e) {
		   return {};
	}
}